<template>
  <div class="app-container">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监听类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择监听类型" clearable style="width: 200px">
          <el-option
            v-for="dict in sys_listener_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listenerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="监听类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="sys_listener_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="值类型" align="center" prop="valueType">
        <template #default="scope">
          <dict-tag :options="sys_listener_value_type" :value="scope.row.valueType" />
        </template>
      </el-table-column>
      <el-table-column label="执行内容" align="center" prop="value" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page="queryParams.pageNum" :limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="监听类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择监听类型">
            <el-option
              v-for="dict in sys_listener_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事件类型" prop="eventType" v-if="form.type === '1'">
          <el-select v-model="form.eventType" placeholder="请选择事件类型">
            <el-option
              v-for="dict in taskListenerEventList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事件类型" prop="eventType" v-else>
          <el-select v-model="form.eventType" placeholder="请选择事件类型">
            <el-option
              v-for="dict in executionListenerEventList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="值类型" prop="valueType">
          <el-radio-group v-model="form.valueType">
            <el-radio
              v-for="dict in sys_listener_value_type"
              :key="dict.value"
              :value="dict.value"
            >
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="执行内容" prop="value">
          <el-input v-model="form.value" placeholder="请输入执行内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { listListener, getListener, delListener, addListener, updateListener } from '@/api/flowable/listener';

const { proxy } = getCurrentInstance();
const {sys_listener_value_type, sys_listener_type, common_status, sys_listener_event_type} = proxy.useDict('sys_listener_value_type', 'sys_listener_type', 'common_status', 'sys_listener_event_type');

const queryFormRef = ref(null);
const formRef = ref(null);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const listenerList = ref([]);
const title = ref('');
const open = ref(false);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  type: null,
  eventType: null,
  valueType: null,
  value: null,
  status: null,
});
const form = reactive({
  id: null,
  name: null,
  type: null,
  eventType: null,
  valueType: null,
  value: null,
  createTime: null,
  updateTime: null,
  createBy: null,
  updateBy: null,
  status: null,
  remark: null,
});
const rules = reactive({});

const taskListenerEventList = reactive([
  { label: 'create', value: 'create' },
  { label: 'assignment', value: 'assignment' },
  { label: 'complete', value: 'complete' },
  { label: 'delete', value: 'delete' },
]);
const executionListenerEventList = reactive([
  { label: 'start', value: 'start' },
  { label: 'end', value: 'end' },
  { label: 'take', value: 'take' },
]);

const route = useRoute();
const router = useRouter();

onMounted(() => {
  getList();
});

const getList = () => {
  loading.value = true;
  listListener(queryParams)
    .then((response) => {
      listenerList.value = response.rows;
      total.value = response.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

const cancel = () => {
  open.value = false;
  reset();
};

const reset = () => {
  Object.assign(form, {
    id: null,
    name: null,
    type: null,
    eventType: null,
    valueType: null,
    value: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    status: null,
    remark: null,
  });
  formRef.value?.resetFields();
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleAdd = () => {
  reset();
  open.value = true;
  title.value = '添加流程监听';
};

const handleUpdate = (row) => {
  reset();
  const id = row.id || ids.value;
  getListener(id).then((response) => {
    Object.assign(form, response.data);
    open.value = true;
    title.value = '修改流程监听';
  });
};

const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.id) {
        updateListener(form)
          .then(() => {
            ElMessage.success('修改成功');
            open.value = false;
            getList();
          })
          .catch(() => {});
      } else {
        addListener(form)
          .then(() => {
            ElMessage.success('新增成功');
            open.value = false;
            getList();
          })
          .catch(() => {});
      }
    }
  });
};

const handleDelete = (row) => {
  const ids = row.id || ids.value;
  ElMessageBox.confirm('是否确认删除流程监听编号为"' + ids + '"的数据项？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      return delListener(ids);
    })
    .then(() => {
      getList();
      ElMessage.success('删除成功');
    })
    .catch(() => {});
};

const handleExport = () => {
  // 实现导出逻辑
};
</script>